{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9a4a5f8f-d1fd-4124-ae11-f6b263c36452",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "# <center>Python程序设计基础<center>\n",
    "## <center>第四章 Python 数据结构<center>\n",
    " <center>沈万福<center>\n",
    " <center> 办公地点：第十教学楼414室<center>\n",
    " <center>tel:18822265292<center>\n",
    " <center>mail:wfshen@tju.edu.cn<center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcb58f74-c20e-4761-87ef-4a233ee58a19",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## <center>内容摘要<center>\n",
    "<center> 一、列表 <cetner>\n",
    "<center> 二、元组  <center>\n",
    "<font size = 9><center> 三、字典   <center></font>\n",
    "<center> 四、集合  <center>\n",
    "    <center>      五、字符串    <center>\n",
    "<center> 六、序列   <center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "731c7a48-36ba-4b0d-930b-5bd50858236c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    },
    "tags": []
   },
   "source": [
    "## 字典\n",
    "-----\n",
    "### <font color = #DC143C> 字典的定义</font>    \n",
    "字典是Python中唯一内建的映射类型，用一对花括号 `{` 和 `}` 作为边界，元素之间以逗号分隔，每个元素是一对键（key）和值（value），键和值之间用英文冒号分隔。字典可通过数据key查找关联数据value。\n",
    "- Python中字典的**元素没有特殊的顺序**，因此不能像序列那样通过位置索引来查找成员数据。\n",
    "- 但是每一个值都有一个对应的键。**字典的用法是通过键key来访问相应的值value。**\n",
    "- **字典的键是不重复的。**\n",
    "- **值可以是任何数据类型**，比如字符串等\n",
    "- **键必须是不可变的**，可以是字符串、元组等，**列表不能作为字典的键**。\r\n",
    "\n",
    " \n",
    "说到字典这个词，大家一定不陌生，读小学的时候每个人基本上都有一本《新华字典》，概念类似。*\r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2d95817-76d9-4c26-a9bf-78088b6767ba",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeb42f7e-e035-4072-a4b7-73f876f17903",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": []
   },
   "source": [
    "-----\n",
    "### <font color = #DC143C> 字典的创建</font>  \n",
    "- (1) 大括号对 `{}`：创建字典时，每一个元素都必须包含键和值两个部分。键和值使用`：`（冒号）分隔，每个元素之间通过，`,`（逗号）分隔，字典中的所有元素都放在`{}`中。如果是空字典，则直接使用{}表示。   \n",
    "  **格式为**: `字典名称 = {key1 : value1, key2 : value2, ..., keyn : valuen}`\n",
    "- (2) `dirct()`函数\n",
    "- (3)`zip()`函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e27610c-8721-4698-aba6-128de192aa78",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': '苹果', 'Banana': '香蕉', 'grape': '葡萄', 'peach': '桃子'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> word = {'apple':'苹果', 'Banana':'香蕉', 'grape':'葡萄', 'peach':'桃子' }\n",
    ">>> word       # 字典word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cddf5666-b8bc-4edf-833e-190343246243",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{101: '小兰', 102: '小柯', 103: '小南', 104: '小新', 105: '小南'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> student = {101:'小兰', 102:'小柯', 103:'小南', 104:'小新', 105:'小南'}\n",
    ">>> student     # 字典student"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a47f28bc-ccc0-4aef-818a-9cbd0103fbb9",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "- 字典word中的键值对都是一一对应的，没有重复的键或者值。\n",
    "- 在student字典中，键103对应的值和键105对应的值是相同的，都是“小南”，这在字典中是允许存在的情况。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9edc158-ecf2-41f5-820a-1273cef2f27e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "**注意**：如果字典中的键出现了相同的情况，则后者的值将会覆盖前者的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "afb2041e-e47b-49d6-8d2a-78aec813e623",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{101: '小兰', 102: '小柯', 103: '小新', 105: '小南'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " student = {101:'小兰', 102:'小柯', 103:'小南', 103:'小新', 105:'小南'}   # 键相同的情况\n",
    " student"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a5f5ac0-8440-4ee1-80bc-1e285b1a3cd0",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": []
   },
   "source": [
    "键103分别对应了“小南”和“小新”，则输出时会使用103:'小新'这一元素，而忽略103:'小南'这个元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9f799d7-0f7f-4e53-bd68-d67f3425df7e",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    },
    "tags": []
   },
   "source": [
    "..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcfe3bb7-0ae0-4186-958f-bb114469efd5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "以给定序列为键，创建值为空的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8c603d1b-048a-4804-9a51-864955c53a87",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{101: None, 102: None, 103: None}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c=dict.fromkeys([101,102,103])\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94c233c8-3cd6-4272-9c09-3b46d5c25c0a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "创建空字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "46cf5063-00af-4020-8a9b-d108dff1940f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e438f32-54cf-4352-b530-81e701ea19d8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "**dict()函数创建字典**。这里键和值之间使用的是 `=`。\n",
    "-注意，键的位置不能加上表示字符串的引号，否则会出错。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "60e31a39-06c8-4f8c-9e25-204d64d88bd1",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ascii = dict(A=65, B=66, C=67, D=68, E=69)\n",
    "ascii"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "73917141-cace-4f2f-9d87-b67cd5ab7682",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A': '小兰', 'B': '小柯', 'C': '小南'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student = dict (A = '小兰', B = '小柯', C = '小南')\n",
    "student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dee6c9d9-423f-4748-a30e-3bd01ebf719f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小兰': 101}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "student = dict (小兰 = 101)\n",
    "student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a18b7081-5b37-44fa-845c-07ca5089aa9c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expression cannot contain assignment, perhaps you meant \"==\"? (1440023943.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[34], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    student = dict( 101 = '小兰', 102 = '小柯', 103 = '小南')\u001b[0m\n\u001b[1;37m                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expression cannot contain assignment, perhaps you meant \"==\"?\n"
     ]
    }
   ],
   "source": [
    "student = dict( 101 = '小兰', 102 = '小柯', 103 = '小南')\n",
    "student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5a182ad8-350d-483f-9a5d-0849987b6ccb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expression cannot contain assignment, perhaps you meant \"==\"? (2151988934.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[35], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    student = dict( 101 = A, 102 = B, 103 = C)\u001b[0m\n\u001b[1;37m                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expression cannot contain assignment, perhaps you meant \"==\"?\n"
     ]
    }
   ],
   "source": [
    "student = dict( 101 = A, 102 = B, 103 = C)\n",
    "student"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "347b9d22-4cbf-4017-99f2-8eecd4356e1b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "expression cannot contain assignment, perhaps you meant \"==\"? (896034633.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[36], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    student = dict( 101 = 1, 102 = 2, 103 = 3)\u001b[0m\n\u001b[1;37m                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expression cannot contain assignment, perhaps you meant \"==\"?\n"
     ]
    }
   ],
   "source": [
    "student = dict( 101 = 1, 102 = 2, 103 = 3)\n",
    "student"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0208cc4-31b9-4b24-987e-24b7e012e40b",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "注意：发现用`dict()`函数是，`key` 不能是数字，具体原因还不知道！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb68b029-ae3c-47a4-b468-c719c8db1f39",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
